package com.ruicar.afs.cloud.account.common.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.account.common.dto.VoucherTemplateEventsDto;
import com.ruicar.afs.cloud.account.common.entity.VoucherTemplateEvents;
import com.ruicar.afs.cloud.account.common.service.VoucherTemplateEventsService;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author infinity
 * @version 1.0
 * @date 2020/7/4 18:34
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/voucherEvents")
@Api(value = "voucherEvents", description = "凭证事件信息")
public class VoucherEventsController {
    private final VoucherTemplateEventsService voucherTemplateEventsService;

    @PostMapping("/addVoucherEvents")
    @ApiOperation(value = "添加凭证事件")
    @SysLog(value = "添加凭证事件")
    public IResponse<Boolean> addVoucherEvents(@RequestBody @Validated VoucherTemplateEventsDto dto) {
        return IResponse.success(voucherTemplateEventsService.addVoucherEvents(dto));
    }

    @DeleteMapping("/delVoucherEvents/{id}")
    @ApiOperation(value = "删除凭证事件")
    @SysLog(value = "删除凭证事件")
    public IResponse<Boolean> delVoucherEvents(@PathVariable String id) {
        return IResponse.success(voucherTemplateEventsService.delVoucherEvents(Long.parseLong(id)));
    }

    @PostMapping("/modifyVoucherEvents")
    @ApiOperation(value = "编辑凭证事件")
    @SysLog(value = "编辑凭证事件")
    public IResponse<Boolean> modifyVoucherEvents(@RequestBody @Validated VoucherTemplateEventsDto dto) {
        return IResponse.success(voucherTemplateEventsService.modifyVoucherEvents(dto));
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询凭证事件")
    @SysLog(value = "查询凭证事件")
    public IResponse list(@RequestBody QueryCondition<VoucherTemplateEventsDto> condition) {
        return voucherTemplateEventsService.queryVoucherEventsList(condition);
    }

    @PostMapping("/listVoucherEvents")
    @ApiOperation(value = "查询凭证事件")
    @SysLog(value = "查询凭证事件")
    public IResponse queryVoucherEventsList(@RequestBody QueryCondition<VoucherTemplateEvents> condition) {
        IPage iPage = voucherTemplateEventsService.page(new Page<>(condition.getPageNumber(), condition.getPageSize()),
                Wrappers.<VoucherTemplateEvents>lambdaQuery()
                        .like(StringUtils.isNotEmpty(condition.getCondition().getMainBusinessCode()), VoucherTemplateEvents::getMainBusinessCode, condition.getCondition().getMainBusinessCode())
                        .like(StringUtils.isNotEmpty(condition.getCondition().getChildBusinessCode()), VoucherTemplateEvents::getChildBusinessCode, condition.getCondition().getChildBusinessCode())
                        .like(StringUtils.isNotEmpty(condition.getCondition().getBuriedPointNo()), VoucherTemplateEvents::getBuriedPointNo, condition.getCondition().getBuriedPointNo())
                        .eq(condition.getCondition().getStatus() != null, VoucherTemplateEvents::getStatus, condition.getCondition().getStatus()));
        return IResponse.success(iPage);
    }

    @PostMapping("/active/{id}")
    @ApiOperation(value = "通过id设置凭证事件信息生效")
    public IResponse active(@PathVariable Long id) {
        return IResponse.success(voucherTemplateEventsService.active(id));
    }

    @PostMapping("/invalidity/{id}")
    @ApiOperation(value = "通过id设置凭证事件信息无效")
    public IResponse invalidity(@PathVariable Long id) {
        return IResponse.success(voucherTemplateEventsService.invalidity(id));
    }

    @PostMapping("/getVoucherEventsInfo/{id}")
    @ApiOperation(value = "通过id获取凭证事件信息")
    public IResponse getVoucherEventsInfo(@PathVariable Long id) {
        return IResponse.success(voucherTemplateEventsService.getById(id));
    }

}
